
local Field = import("./Field.lua");
local Input = commonlib.inherit(Field);


function Input:Init(block, opt)
    Field._super.Init(self, block, opt);

    self:SetCanEdit(true);
    self:SetValue(opt.value or "");
    
    return self;
end

function Input:Render()
    local leftUnitCount, topUnitCount = self:GetLeftTopUnitCount();
    local widthUnitCount, heightUnitCount = self:GetWidthHeightUnitCount();
    self:SetBrushColor(0xffffffff);
    self:DrawInputValue(widthUnitCount, heightUnitCount, leftUnitCount, topUnitCount);
    self:DrawText(self:GetText(), widthUnitCount - heightUnitCount, heightUnitCount, leftUnitCount + heightUnitCount / 2, topUnitCount);
end

function Input:UpdateWidthHeightUnitCount()
    local text = self:GetValue();
    local widthUnitCount = self:GetTextWidthUnitCount(text);
    if (widthUnitCount > self.FIELD_MAX_WIDTH_UNIT_COUNT) then
        widthUnitCount = self.FIELD_MAX_WIDTH_UNIT_COUNT;
        text = self:GetTextByWidth(text, widthUnitCount);
    end
    self:SetText(text);
    return widthUnitCount + self.FIELD_INPUT_HEIGHT_UNIT_COUNT, self.FIELD_INPUT_HEIGHT_UNIT_COUNT;
end



return Input;